﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TDT_Project.classes;


namespace TDT_Project.Controller
{
    public partial class uc_Nhan_Xet : System.Web.UI.UserControl
    {
        cls_phuhuynhxemdiem cls = new cls_phuhuynhxemdiem();
        private DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        private DataSet ds_nhanxat = new DataSet();
        private string nienkhoa = "";
        private string giaovien = "";
        private int loai;
        private string hocsinh = "";
        private string ten_lop = "";
        private string lop_id = "";
        private string hoc_ky = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["nk"] != null)
                    {
                        txtghichu_PH.Visible = false;
                        btn_gopy_PH.Visible = false;
                        lblGopY.Text = "";
                        nienkhoa = Session["nk"].ToString();
                        giaovien = Session["gv"].ToString();
                       // loai = Convert.ToInt32(Session["Loai"].ToString());
                        load_svien();
                        ds = cls.Get_Thong_Tin_Hoc_Nhan_Xet(nienkhoa, giaovien);
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            ten_lop = ds.Tables[0].Rows[0]["Ten_Lop"].ToString();
                            Session["lop"] = ds.Tables[0].Rows[0]["Lop_Id"].ToString();
                            lbl_Tenlop.Text = ten_lop;
                        }
                        gvResult_TTT.DataSource = dt;
                        gvResult_TTT.DataBind();
                    }
                    else
                        Response.Redirect("Default.aspx");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        private void load_svien()
        {
            ds = cls.Get_Thong_Tin_Hoc_Nhan_Xet(nienkhoa, giaovien);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                cbo_Hocsinh.DataSource = ds;
                cbo_Hocsinh.DataTextField = "hoten";
                cbo_Hocsinh.DataValueField = "HocSinh_ID";
                cbo_Hocsinh.DataBind();
            }
        }
        protected void gvResult_TTT_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            Telerik.Web.UI.GridDataItem item = (Telerik.Web.UI.GridDataItem)e.Item;
            if (e.CommandName.Equals("lnkhs_ID"))
            {
                Session["hs"] = e.CommandArgument.ToString();
                Session["lop"] = item.GetDataKeyValue("Lop_Id").ToString();
                Session["nk"] = item.GetDataKeyValue("NienHoc_ID").ToString();
                Session["hk"] = "HK1";
                Session["Loai"] = 1;
                Response.Redirect("~/xemdiem.aspx?pages=xemdiem");
            }
        }

        protected void gvResult_TTT_SelectedCellChanged(object sender, EventArgs e)
        {

        }

        protected void btn_gopy_PH_Click(object sender, EventArgs e)
        {
            if (Session["nk"] != null)
            {

                hocsinh = cbo_Hocsinh.SelectedValue.ToString();
                hoc_ky = "HK1";
                nienkhoa = Session["nk"].ToString();
                lop_id = Session["lop"].ToString();
                if (hocsinh != null && nienkhoa != null && hoc_ky != null && lop_id != null)
                {
                    if (txtghichu_PH.Text != string.Empty)
                    {
                        bool insert = cls.Insert_NhanXet(lop_id.ToString(), nienkhoa.ToString(), hoc_ky.ToString(), DateTime.Now, txtghichu_PH.Text, 1, hocsinh.ToString());
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông Báo", "<script>alert('Góp ý thành công!')</script>");
                        txtghichu_PH.Text = "";
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
            else
                Response.Redirect("Default.aspx");
        }


        protected void cbo_Hocsinh_SelectedIndexChanged1(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                if (Session["nk"] != null)
                {

                    hocsinh = cbo_Hocsinh.SelectedValue.ToString();
                    nienkhoa = Session["nk"].ToString();
                    loai = 0;
                    lop_id = Session["lop"].ToString();
                    hoc_ky = "HK1";
                    ds_nhanxat = cls.TOAN_Get_Thong_Tin_Nhan_Xet(hocsinh, lop_id, nienkhoa, hoc_ky, loai);
                    if (ds_nhanxat != null && ds_nhanxat.Tables.Count > 0)
                    {
                        if (ds_nhanxat.Tables[0].Rows.Count > 0)
                        {
                            gvResult_TTT.DataSource = ds_nhanxat.Tables[0].DefaultView;
                            gvResult_TTT.DataBind();
                            txtghichu_PH.Visible = true;
                            btn_gopy_PH.Visible = true;
                            lblGopY.Text = "Ý kiến nhận xét của giáo viên chủ nhiệm:";
                        }
                        else
                        {
                            gvResult_TTT.DataSource = dt;
                            gvResult_TTT.DataBind();
                            txtghichu_PH.Visible = false;
                            btn_gopy_PH.Visible = false;
                            lblGopY.Text = "";
                        }
                    }
                    else
                    {
                        gvResult_TTT.DataSource = dt;
                        gvResult_TTT.DataBind();
                        txtghichu_PH.Visible = false;
                        btn_gopy_PH.Visible = false;
                        lblGopY.Text = "";
                    }
                }
                else
                    Response.Redirect("Default.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}